#include "GameObject.h"
#include "Application.h"
#include "hgeAnim.h"

GameObject::GameObject()
: m_pos(0,0)
, m_vel(0,0)
, m_speed(1)
, m_hTexture(NULL)
, m_pSprite(NULL)
{
}
GameObject::~GameObject()
{
	if(m_pSprite)
	{
		delete m_pSprite;
		m_pSprite = NULL;
	}
	if(m_hTexture)
	{
		Application::GetInstance().GetHGE()->Texture_Free(m_hTexture);
	}
}

bool GameObject::CreateObject(char* a_pSpriteName, int a_width, int a_height)
{
	m_dimensions.x = (float)a_width;
	m_dimensions.y = (float)a_height;

	m_boundingCircle.radius = m_dimensions.x/2;

	m_pSprite = new hgeSprite(*Application::GetInstance().GetResourceManager()->GetSprite(a_pSpriteName));
	if(!m_pSprite)
		return false;

	return true;
}

bool GameObject::Update(float a_dt)
{
	//Normalize the velocity
	m_vel.Normalize();

	//scale by the speed
	m_vel *= m_speed;
	
	//Apply to the position
	m_pos += m_vel * a_dt;

	for(std::map<int, hgeAnimation*>::iterator iter = m_animations.begin(); iter != m_animations.end(); ++iter)
	{
		hgeAnimation* pElem = iter->second;
		if(pElem->IsPlaying())
			pElem->Update(a_dt);
	}
	
	return true;
}

void GameObject::Render()
{
	m_pSprite->Render(m_pos.x, m_pos.y);
}

hgeVector GameObject::GetPos() const
{
	return m_pos;
}
hgeVector GameObject::GetDimensions() const
{
	return m_dimensions;
}
hgeSprite* GameObject::GetSprite() const
{
	return m_pSprite;
}
BoundingCircle GameObject::GetBoundingCircle() const
{
	return m_boundingCircle;
}
void GameObject::SetPos(hgeVector a_pos)
{
	m_pos = a_pos;
}
void GameObject::SetPos(float a_xPos, float a_yPos)
{
	m_pos.x = a_xPos;
	m_pos.y = a_yPos;
}
void GameObject::SetSubRect(float a_x, float a_y, float a_width, float a_height)
{
	m_pSprite->SetTextureRect(a_x, a_y, a_width, a_height);
}
void GameObject::SetCenter(float a_x, float a_y)
{
	m_pSprite->SetHotSpot(a_x, a_y);
}
void GameObject::SetVelocity(hgeVector a_vel)
{
	//The velocity is normalized before being used, so add each velocity onto this
	//As you can add x and y velocity seperately with different buttons
	m_vel += a_vel;
}
void GameObject::SetSpeed(float a_speed)
{
	m_speed = a_speed;
}
hgeAnimation* GameObject::GetAnimation(int a_key) const
{
	return m_animations.find(a_key)->second;
}
void GameObject::Kill()
{

}
void GameObject::SetBoundingCircle(BoundingCircle a_circle)
{
	m_boundingCircle = a_circle;
}
